package com.lrkj.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.entity.TbFile;
import com.lrkj.back.service.TbFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.cbons.mamisay.parent.exception.BusinessException;

@Controller
@RequestMapping("/file")
public class TbFileController extends BaseController{
    @Autowired
    private TbFileService fileService;
    
    @RequestMapping("/xinjian.do")
    public ModelAndView xinjian(Integer id, HttpServletRequest request,HttpServletResponse response) throws BusinessException{
    	TbFile file= (id == null) ? new TbFile() : fileService.selectByPrimaryKey(id);
		request.setAttribute("shift", file);
    	return new ModelAndView("/system/wenku/xinjian_file");
    }
    
    @RequestMapping("/fileSave.do")
	@ResponseBody
    public Map<Object,Object> fileSave(TbFile file, HttpServletRequest request,HttpServletResponse response) throws BusinessException{
    	Map<Object, Object> rtnMap = new HashMap<Object,Object>();
    	Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (file!=null) {
	    		map.put("fileName",file.getFileName());
			}
			List<TbFile> files = fileService.findAll(map);
			file.setCreateTime(new Date());
			if (files.isEmpty()) {
				if(file.getFileId() != null){
					fileService.updateByPrimaryKey(file);
				}else{
					fileService.insert(file);
				}
				rtnMap.put("status", "true");
				rtnMap.put("msg", "编辑成功");
			}else {
				rtnMap.put("status", "false");
				rtnMap.put("msg", "已存在该文件夹");
			}
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
    }
    
    @RequestMapping("/fileEdit.do")
	@ResponseBody
	public Map<Object,Object> shiftSave(TbFile file, String wj, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			file.setCreateTime(new Date());
			if(file.getFileId()!= null){
				fileService.updateByPrimaryKey(file);
			}else{
				fileService.insert(file);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
}
